#!/bin/bash
set -o errexit

WORKDIR=$(pwd)
cd $WORKDIR

REPODIR="/tmp/jellyfin-plugin-repo"
version=$(echo -n $TAG | sed "s/v//g")
changetime=$(date +%Y-%m-%dT%H:%M:%SZ)
changelog=$(git show -s --format=%s)
targetAbi="10.7.0.0"
mirror_us="https://github.com/caryyu/jellyfin-plugin-repo/raw/master/Jellyfin.Plugin.OpenDouban.$version.zip"
DLL_PATH="$WORKDIR/Jellyfin.Plugin.OpenDouban/bin/Debug/net5.0/Jellyfin.Plugin.OpenDouban.dll"
PKG_NAME="$REPODIR/Jellyfin.Plugin.OpenDouban.$version.zip"

setup() {
    echo -n ${SSH_PRIVKEY_GITHUB} | base64 -d > /tmp/github-privkey
    chmod 400 /tmp/github-privkey

    eval $(ssh-agent)
    mkdir -p ~/.ssh
    ssh-add -k /tmp/github-privkey
    ssh-keyscan -t rsa github.com > ~/.ssh/known_hosts

    git clone git@github.com:caryyu/jellyfin-plugin-repo.git $REPODIR
    cd $REPODIR

    git remote -v
    ls -la

    zip -j ${PKG_NAME} ${DLL_PATH}
    ls -la
    cd $WORKDIR
}

change_manifest_file() {
    local jsonfile=$1
    local mirror=$2
    echo "Update $jsonfile: version: $version"
    cd $REPODIR
    local isUpdate="false"
    local versions=()
    for row in $(cat $jsonfile | jq -r '.[0].versions[] | @base64'); do
        _jq() {
            echo ${row} | base64 --decode | jq -r ${1}
        }

        if [ "$version" == "$(_jq '.version')" ]; then
            isUpdate="true"
            versions+=("{
                \"checksum\": \"$checksum\",
                \"changelog\": \"$changelog\",
                \"targetAbi\": \"$targetAbi\",
                \"sourceUrl\": \"$mirror\",
                \"timestamp\": \"$changetime\",
                \"version\": \"$version\"
            }")
        else
            versions+=("{
                \"checksum\": \"$(_jq '.checksum')\",
                \"changelog\": \"$(_jq '.changelog')\",
                \"targetAbi\": \"$(_jq '.targetAbi')\",
                \"sourceUrl\": \"$(_jq '.sourceUrl')\",
                \"timestamp\": \"$(_jq '.timestamp')\",
                \"version\": \"$(_jq '.version')\"
            }")
        fi
        
    done

    if [ "$isUpdate" == "false" ]; then
        versions+=("{
            \"checksum\": \"$checksum\",
            \"changelog\": \"$changelog\",
            \"targetAbi\": \"$targetAbi\",
            \"sourceUrl\": \"$mirror\",
            \"timestamp\": \"$changetime\",
            \"version\": \"$version\"
        }")
    fi

    JSON=[$(IFS=,; printf '%s' "${versions[*]}")]
    cat $jsonfile | jq --argjson versions "$JSON" '.[0].versions = $versions' > /tmp/txt
    mv /tmp/txt $jsonfile
    cat $jsonfile
    cd $WORKDIR
}

trigger_git_push() {
    cd $REPODIR
    git config --global push.default matching
    git config --global user.email "caryyu.tg@gmail.com"
    git config --global user.name "caryyu"
    git stage .
    git status
    git commit -m "release jellyfin-plugin-douban $version"
    git push origin
    cd $WORKDIR
}

setup
checksum=$(cat $PKG_NAME | md5sum | cut -d' ' -f1)
change_manifest_file "manifest-us.json" $mirror_us
trigger_git_push
